package com.nineclock.document.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nineclock.common.threadlocal.CurrentUserHolder;
import com.nineclock.common.utils.BeanHelper;
import com.nineclock.document.dto.DocFileDto;
import com.nineclock.document.dto.DocFolderDto;
import com.nineclock.document.dto.DocumentDto;
import com.nineclock.document.mapper.DocFileMapper;
import com.nineclock.document.mapper.DocFolderMapper;
import com.nineclock.document.pojo.DocFile;
import com.nineclock.document.pojo.DocFolder;
import com.nineclock.document.service.DocumentService;
import com.nineclock.system.dto.SysCompanyUserDTO;
import com.nineclock.system.feign.SysUserFeign;
import com.nineclock.system.pojo.SysCompanyUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

import com.nineclock.common.enums.ResponseEnum;
import com.nineclock.common.exception.NcException;
import com.nineclock.common.threadlocal.CurrentUserHolder;
import com.nineclock.common.utils.CountUtil;
import com.nineclock.document.dto.DocFileDto;
import com.nineclock.document.dto.DocFolderDto;
import com.nineclock.document.dto.DocumentDto;
import com.nineclock.document.mapper.DocFolderMapper;
import com.nineclock.document.mapper.DocumentMapper;
import com.nineclock.document.pojo.DocFile;
import com.nineclock.document.pojo.DocFolder;
import com.nineclock.document.service.DocumentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.plugin.dom.core.Document;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
@SuppressWarnings("all")
public class DocumentServiceImpl implements DocumentService {

    @Autowired
    private DocFileMapper docFileMapper;
    @Autowired
    private DocFolderMapper docFolderMapper;
    @Autowired
    private SysUserFeign sysUserFeign;


    @Override
    public DocumentDto queryDocument(Long parentFoldId, String keyword) {

        //如果没有获取到说明显示主界面的所有文件和文件夹
        if (parentFoldId==null){
            parentFoldId=0L;
            return getDocumentDto(parentFoldId, keyword);
        }else {
           return getDocumentDto(parentFoldId,keyword);
        }

    }


    @Autowired
    DocumentMapper documentMapper;

    @Override
    public void createDoc(DocFileDto docFileDto) {
        if(docFileDto==null){
            throw new NcException(ResponseEnum.INVALID_PARAM_ERROR);
        }
        DocFile docFile=new DocFile();
        // 设置前端传递过来的参数
        //内容
        docFile.setContent(docFileDto.getContent());
        //名字
        docFile.setName(docFileDto.getName());
        //设置创建用户id
        docFile.setCreateUserId(CurrentUserHolder.get().getCompanyUserId());
        //设置企业id
        docFile.setCompanyId(CurrentUserHolder.get().getCompanyId());
        //创建时间 表中有默认值
        docFile.setCreateTime(new Date());
        //文件路径
        docFile.setFilePath(docFileDto.getFilePath());
        //文件夹id
        docFile.setFolderId(docFileDto.getFolderId());
        //更新用户id
        docFile.setUpdateUserId(CurrentUserHolder.get().getCompanyUserId());
        //设置文件大小
        byte[] bytes = docFileDto.getContent().getBytes();
        String size = CountUtil.getSize(bytes.length);
        docFile.setFileSize(size);



        documentMapper.insert(docFile);
    }

    @Override
    public void createFolder(DocFolderDto docFolderDto) {
        if(docFolderDto==null){
            throw new NcException(ResponseEnum.INVALID_PARAM_ERROR);
        }
        DocFolder docFolder=new DocFolder();
        docFolder.setCompanyId(CurrentUserHolder.get().getCompanyId());
        docFolder.setCreateTime(new Date());
        docFolder.setCreateUserId(CurrentUserHolder.get().getCompanyUserId());
        docFolder.setName(docFolderDto.getName());
        docFolder.setParentId(docFolderDto.getParentId());
        docFolderMapper.insert(docFolder);
    }

    private DocumentDto getDocumentDto(Long parentFoldld, String keyword) {
        LambdaQueryWrapper<DocFile> wrapper1 = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<DocFolder> wrapper2 = new LambdaQueryWrapper<>();
        //查询所有文件
        wrapper1.eq(DocFile::getCompanyId, CurrentUserHolder.get().getCompanyId());
        wrapper1.eq(DocFile::getFolderId,parentFoldld);
        wrapper1.eq(DocFile::getStatus,1);
        wrapper1.like(DocFile::getName,keyword);
        List<DocFile> docFiles = docFileMapper.selectList(wrapper1);


        //查询所有文件夹
        wrapper2.eq(DocFolder::getCompanyId,CurrentUserHolder.get().getCompanyId());
        wrapper2.eq(DocFolder::getParentId,parentFoldld);
        wrapper2.eq(DocFolder::getStatus,1);
        wrapper2.like(DocFolder::getName,keyword);
        List<DocFolder> docFolders = docFolderMapper.selectList(wrapper2);
        //添加至返回值
        DocumentDto documentDto = new DocumentDto();
        List<DocFileDto> files = BeanHelper.copyWithCollection(docFiles, DocFileDto.class);
            for (DocFileDto file : files) {

                //用户名
                file.setUserName(CurrentUserHolder.get().getUsername());


            }
        documentDto.setFiles(files);

        List<DocFolderDto> folders = BeanHelper.copyWithCollection(docFolders, DocFolderDto.class);
        documentDto.setFolders(folders);
        //返回
        return documentDto;
    }
}

